package com.clearance.mine_clearance.untils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class SendRedPacketUntils {
    public static void main(String[] args) {
        List<BigDecimal> bigDecimals = sendRedPacket(BigDecimal.valueOf(60.8), 6,3);
        System.out.println(bigDecimals);
    }


    public static List<BigDecimal> sendRedPacket(BigDecimal totalAmount, int num,int lei) {
        List<BigDecimal> amounts = new ArrayList<>();
        Random random = new Random();
        // 生成 num - 1 个随机数，表示 num - 1 个红包的金额
        BigDecimal leftAmount = totalAmount;
        for (int i = 0; i < num - 1; i++) {
            BigDecimal amount = new BigDecimal(random.nextDouble() * (leftAmount.subtract(leftAmount.divide(BigDecimal.valueOf(2)))).doubleValue()).setScale(2, BigDecimal.ROUND_HALF_UP);
            if(amount.doubleValue()==0||amount.doubleValue()==leftAmount.doubleValue()){
                while (amount.doubleValue()==0){
                    amount = new BigDecimal(random.nextDouble() * leftAmount.doubleValue()).setScale(2, BigDecimal.ROUND_HALF_UP);
                    amounts.add(amount);
                    leftAmount = leftAmount.subtract(amount);
                }
            }else {
                amounts.add(amount);
                leftAmount = leftAmount.subtract(amount);
            }
        }
        amounts.add(leftAmount);
        // 随机生成最后一个红包的金额，直到满足至少有一个数字为 5
        Collections.shuffle(amounts);
        return amounts;
    }
    // 检查金额是否满足至少有一个数字为 5
    private static boolean checkAmount(BigDecimal amount) {
        return amount.multiply(new BigDecimal("100")).intValue() % 10 == 5;
    }
}
